package fr.upmc.i3s.upmcontact.content.collectors;

import fr.upmc.i3s.upmcontact.tools.PreferencesManager;
import android.content.Context;

public class UpdateDatesManager {
	
	private static final long MIN_UPDATE_INTERVAL = 1000*60*60*2; // 2H -> ms
	
	private static final String UPDATE_DATE_GLOBAL 			= "fr.upmc.i3s.upmcontact.update.date.global";
	private static final String UPDATE_DATE_LOST 			= "fr.upmc.i3s.upmcontact.update.date.lost";
	private static final String UPDATE_DATE_FOUND 			= "fr.upmc.i3s.upmcontact.update.date.found";
	private static final String UPDATE_DATE_UPMC 			= "fr.upmc.i3s.upmcontact.update.date.upmc";
	private static final String UPDATE_DATE_STUDENT 		= "fr.upmc.i3s.upmcontact.update.date.student";
	private static final String UPDATE_DATE_MERCHANTS 		= "fr.upmc.i3s.upmcontact.update.date.merchants";
	private static final String UPDATE_DATE_CHAT_MESSAGES 	= "fr.upmc.i3s.upmcontact.update.date.chat.messages";
	
	// storage /////////////////////////
	
	public static void storeGlobalUpdateDate(Context context) {
		long dateMs = System.currentTimeMillis();
		PreferencesManager.storeDate(context, UPDATE_DATE_GLOBAL, dateMs);
	}
	
	public static void storeLostObjectsUpdateDate(Context context) {
		long dateMs = System.currentTimeMillis();
		PreferencesManager.storeDate(context, UPDATE_DATE_LOST, dateMs);
	}
	
	public static void storeFoundObjectsUpdateDate(Context context) {
		long dateMs = System.currentTimeMillis();
		PreferencesManager.storeDate(context, UPDATE_DATE_FOUND, dateMs);
	}
	
	public static void storeUpmcInfosUpdateDate(Context context) {
		long dateMs = System.currentTimeMillis();
		PreferencesManager.storeDate(context, UPDATE_DATE_UPMC, dateMs);
	}
	
	public static void storeStudentLifeInfosUpdateDate(Context context) {
		long dateMs = System.currentTimeMillis();
		PreferencesManager.storeDate(context, UPDATE_DATE_STUDENT, dateMs);
	}
	
	public static void storeMerchantsInfosUpdateDate(Context context) {
		long dateMs = System.currentTimeMillis();
		PreferencesManager.storeDate(context, UPDATE_DATE_MERCHANTS, dateMs);
	}
	
	public static void storeChatMessagesUpdateDate(Context context) {
		long dateMs = System.currentTimeMillis();
		PreferencesManager.storeDate(context, UPDATE_DATE_CHAT_MESSAGES, dateMs);
	}
	
	// retrieving /////////////////////////
	
	public static long retrieveUpdateGlobalDate(Context context) {
		return PreferencesManager.retrieveDate(context, UPDATE_DATE_GLOBAL);
	}
	
	public static long retrieveUpdateLostObjectsDate(Context context) {
		return PreferencesManager.retrieveDate(context, UPDATE_DATE_LOST);
	}
	
	public static long retrieveUpdateFoundObjectsDate(Context context) {
		return PreferencesManager.retrieveDate(context, UPDATE_DATE_FOUND);
	}
	
	public static long retrieveUpdateStudentInfosDate(Context context) {
		return PreferencesManager.retrieveDate(context, UPDATE_DATE_STUDENT);
	}
	
	public static long retrieveUpdateUpmcInfosDate(Context context) {
		return PreferencesManager.retrieveDate(context, UPDATE_DATE_UPMC);
	}
	
	public static long retrieveUpdateMerchantsInfosDate(Context context) {
		return PreferencesManager.retrieveDate(context, UPDATE_DATE_MERCHANTS);
	}
	
	public static long retrieveUpdateChatMessageDate(Context context) {
		return PreferencesManager.retrieveDate(context, UPDATE_DATE_CHAT_MESSAGES);
	}
	
	// testing ////////////////////////////
	
	public static boolean shouldUpdateGlobal(Context context) {
		long dateMs = System.currentTimeMillis();
		long savedDate = PreferencesManager.retrieveDate(context, UPDATE_DATE_GLOBAL);
		return (savedDate == 0) ? true : (dateMs-savedDate)>MIN_UPDATE_INTERVAL;
	}
	
	public static boolean shouldUpdateLostObjects(Context context) {
		long dateMs = System.currentTimeMillis();
		long savedDate = PreferencesManager.retrieveDate(context, UPDATE_DATE_LOST);
		return (savedDate == 0) ? true : (dateMs-savedDate)>MIN_UPDATE_INTERVAL;
	}
	
	public static boolean shouldUpdateFoundObjects(Context context) {
		long dateMs = System.currentTimeMillis();
		long savedDate = PreferencesManager.retrieveDate(context, UPDATE_DATE_FOUND);
		return (savedDate == 0) ? true : (dateMs-savedDate)>MIN_UPDATE_INTERVAL;
	}
	
	public static boolean shouldUpdateUpmcInfos(Context context) {
		long dateMs = System.currentTimeMillis();
		long savedDate = PreferencesManager.retrieveDate(context, UPDATE_DATE_UPMC);
		return (savedDate == 0) ? true : (dateMs-savedDate)>MIN_UPDATE_INTERVAL;
	}
	
	public static boolean shouldUpdateStudentLifeInfos(Context context) {
		long dateMs = System.currentTimeMillis();
		long savedDate = PreferencesManager.retrieveDate(context, UPDATE_DATE_STUDENT);
		return (savedDate == 0) ? true : (dateMs-savedDate)>MIN_UPDATE_INTERVAL;
	}
	
	public static boolean shouldUpdateMerchantsInfos(Context context) {
		long dateMs = System.currentTimeMillis();
		long savedDate = PreferencesManager.retrieveDate(context, UPDATE_DATE_MERCHANTS);
		return (savedDate == 0) ? true : (dateMs-savedDate)>MIN_UPDATE_INTERVAL;
	}
	
	public static boolean shouldUpdateChatMessages(Context context) {
		long dateMs = System.currentTimeMillis();
		long savedDate = PreferencesManager.retrieveDate(context, UPDATE_DATE_CHAT_MESSAGES);
		return (savedDate == 0) ? true : (dateMs-savedDate)>MIN_UPDATE_INTERVAL;
	}

}
